package edu.utdallas.cs.securechat.utils;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Date;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import edu.utdallas.cs.securechat.main.ClientProtocol;
import edu.utdallas.cs.securechat.main.ServerProtocol;
import edu.utdallas.cs.securechat.main.TicketRequest;

public class PTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ServerProtocol sp = new ServerProtocol();
		ClientProtocol cp = new ClientProtocol("Chris");
		ClientProtocol bp = new ClientProtocol("Bill");
		try {
			
			sp.generateRSAKeys();
			sp.setSecretKey(sp.generateAESKey());
			cp.generateRSAKeys();
			
			Key chrisPublic = cp.getPublicKey();
			Key billPublic = bp.getPublicKey();
			cp.keyFromPassword("hi".toCharArray());
			SecretKey billKey = sp.generateAESKey();
			SecretKey chrisKey = sp.generateAESKey();
			bp.setSecretKey(billKey);
			cp.setSecretKey(chrisKey);
			
			byte[] TGT = sp.grantTGT(chrisKey, "Chris");
			cp.HandleTGT(TGT);
			
			//byte[] text = new byte[] {87, 79, 87, 46, 46, 46};
			//byte[] cipher = cp.RSAEncrypt(text, sp.getPublicKey());
			//byte[] text2 = sp.RSADecrypt(cipher);
			long timestamp = new Date().getTime();
			byte[] ticket = cp.requestTicket("Bill", timestamp);
						
			TicketRequest cb = sp.handleTicketRequest(ticket);
			ticket = sp.grantTicket(cb, billKey);
			cp.handleTicketResponse(ticket, timestamp);
			timestamp = new Date().getTime();
			
			ticket = cp.authenticateToClient("Bill", timestamp);
			ticket = bp.checkUserAuthentication(ticket, chrisPublic);
			cp.confirmUserConnection(ticket, "Bill", billPublic, timestamp);
			
			byte[] message = cp.encryptMessage("Howdy bill, hows it goin?".getBytes(), "Bill");
			byte[] signedMessage = cp.signMessage(message);
			byte[] message2 = bp.checkMessage(signedMessage, "Chris");
			System.out.println(new String(message2));
			System.out.println(bp.decryptMessage(message2, "Chris"));
			
			
		
			
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
